import { parseToObject } from '@/utils';
import { INITIAL_FORM_QUERY_VALUE } from '../../constants';

export default {
  name: 'store-settings-header-form',
  props: {
    topChannelList: {
      type: Array,
      default: () => [],
    },
    // 表单默认值
    defaultFormQueryValueStr: String,
    querying: {
      type: Boolean,
      default: false,
    },
    cellularList: {
      type: Array,
      default: () => [],
    },
    areaList: {
      type: Array,
      default: () => [],
    },
    // 省市区
    cityList: {
      type: Array,
      default: () => [],
    },
  },

  data() {
    return {
      formQuery: { ...INITIAL_FORM_QUERY_VALUE },
      topChannelMap: null,
      labelWidth: '100px',
      okBtnLoading: false,
      cellulars: [],
    };
  },
  watch: {
    // 监听传入的表单默认值变化
    defaultFormQueryValueStr(newValue) {
      if (!newValue) {
        return;
      }
      this.formQuery = {
        ...this.formQuery,
        ...parseToObject(newValue),
      };
    },
  },
  methods: {
    onQuery() {
      this.$emit('onQuery', {
        ...this.formQuery,
        city: this.formQuery.city && this.formQuery.city.length > 0 ? this.formQuery.city[this.formQuery.city.length - 1] : undefined,
      });
    },
    reset() {
      this.formQuery = { ...INITIAL_FORM_QUERY_VALUE };
    },
    topChannelChange(code) {
      this.cellulars = [];
      if (code) {
        this.cellularList.forEach(item => {
          if (item.topChannelCode === code) {
            this.cellulars.push(item);
          }
        });
      }
      this.formQuery = { ...this.formQuery, cellularCode: null };
    },
  },
  async mounted() {},
};
